﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using CellphoneRepair.DAL;
using CellphoneRepair.Entities;
using CellphoneRepair.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;

namespace CellphoneRepair.UserControls
{
    public partial class Uc_peijian : DevExpress.XtraEditors.XtraUserControl
    {
        public Uc_peijian()
        {
            InitializeComponent();

            Load += Uc_peijian_Load;
        }

        private void Uc_peijian_Load(object sender, EventArgs e)
        {
            LoadNav();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            EditType et = new EditType();

            if (et.ShowDialog() == DialogResult.OK)
            {
                LoadNav();
            }
        }

        private void LoadNav()
        {
            navBarGroup1.ItemLinks.Clear();
            using (PjtypeBll bll = new PjtypeBll())
            {
                var list = bll.GetAll();
                foreach (var obj in list)
                {
                    NavBarItem nbi = new NavBarItem(obj.Tpname);
                    nbi.Tag = obj.Idpjtype;
                    nbi.LinkClicked += nbi_LinkClicked;
                    navBarGroup1.ItemLinks.Add(nbi);
                }
            }
        }

        private string id_tp = String.Empty;

        private void nbi_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            id_tp = e.Link.Item.Tag.ToString();

            using (PeijianBll bll = new PeijianBll())
            {
                var list = bll.GetByCondition("*", string.Format(" AND Idpjtype='{0}'", id_tp));

                bindingSource1.DataSource = list;
            }
        }

        private EnumEditType edittype { get; set; }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            if (navBarGroup1.ItemLinks.Count > 0)
            {
                edittype = EnumEditType.Create;

                gridView1.AddNewRow();
                gridView1.ShowEditForm();
            }
        }

        private void bindingSource1_AddingNew(object sender, AddingNewEventArgs e)
        {
            Peijian pj = new Peijian();
            pj.Idpeijian = Commons.GenerateIdByForeTag("PJ");
            pj.Idpjtype = id_tp;
            e.NewObject = pj;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            edittype = EnumEditType.Edit;

            gridView1.ShowEditForm();
        }

        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            using (PeijianBll bll = new PeijianBll())
            {
                switch (edittype)
                {
                    case EnumEditType.Create:
                        bll.Insert(bindingSource1.Current as Peijian);
                        break;

                    case EnumEditType.Edit:
                        bll.Update(bindingSource1.Current as Peijian);
                        break;
                }
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (bindingSource1.List.Count > 0)
            {
                if (XtraMessageBox.Show("是否确定要删除记录？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    using (PeijianBll bll = new PeijianBll())
                    {
                        bll.Delete(bindingSource1.Current as Peijian);
                        bindingSource1.RemoveCurrent();
                    }
                }
            }
        }
    }
}